﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AST.Model
{
    public class BaseEntity
    {
    }

    public class EntityList<T> where T : BaseEntity
    {
        private List<T> _itemList;
        public List<T> ItemList
        {
            get
            {
                if (_itemList == null)
                {
                    _itemList = new List<T>();
                }
                return _itemList;
            }
            set
            {
                _itemList = value;
            }
        }

        public int TotCount { get; set; }

        public int MaxPage { get; set; }
    }


    public static class DBUtility
    {
        public static object GetDBValue<T>(this Nullable<T> param) where T : struct
        {
            if (param.HasValue)
                return param.Value;
            else
                return DBNull.Value;
        }

        public static object GetDBValue(this object param)
        {
            if (param == null)
                return DBNull.Value;
            else
                return param;
        }

        public static object GetDBValueForTime(this object param)
        {
            try
            {
                if (Convert.ToDateTime(param) == DateTime.MinValue)
                    return DBNull.Value;
                else
                    return param;
            }
            catch
            {
                return DBNull.Value;
            }
        }

        public static string FormatValueForString(this string param)
        {
            try
            {
                if (string.IsNullOrEmpty(param))
                {
                    return "";
                }
                else
                {
                    return param;
                }
            }
            catch
            {
                return "";
            }
        }
        public static string FormatValueForDateTime(this DateTime param)
        {
            try
            {
                if (Convert.ToDateTime(param) == DateTime.MinValue)
                    return "";
                else
                    return param.ToString();
            }
            catch
            {
                return "";
            }
        }
        public static string FormatValueForDate(this DateTime param)
        {
            try
            {
                if (Convert.ToDateTime(param) == DateTime.MinValue)
                    return "";
                else
                    return param.ToShortDateString();
            }
            catch
            {
                return "";
            }
        }
    }
}
